package com.ailiwu.www.ailiwu.Fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.activity.CollectionRecylerActivity;
import com.ailiwu.www.ailiwu.adapter.StrategyGridAdapter;
import com.ailiwu.www.ailiwu.adapter.StrategyRecyclerViewAdapter;
import com.ailiwu.www.ailiwu.base.BaseFragment;
import com.ailiwu.www.ailiwu.bean.ClassifyChannelsRecyc;
import com.ailiwu.www.ailiwu.bean.ClassifySubjectSc;
import com.ailiwu.www.ailiwu.url.Urls;
import com.ailiwu.www.ailiwu.utils.Logger;
import com.ailiwu.www.ailiwu.utils.ToastUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

/**
 * Created by zzw on 2015/12/3.
 */
public class ClassifyStrategyFragmnet extends BaseFragment {
    private RecyclerView mSubRecyclerView, mTypeRecyclerView, mCharacterRecyclerView, mSceneRecyclerView, mStyleRecyclerView;
    private TextView mTvStrategy,mTvType,mTvCharacter,mTvScene,mTvStyle;
    private List<ClassifyChannelsRecyc.DataEntity.ChannelGroupsEntity> channelGroups;


    //专题Josn数据
    private ClassifySubjectSc cfss;
    private StrategyRecyclerViewAdapter mSubAdapter;
    //ChannelGroups总Json数据
    private ClassifyChannelsRecyc mCfcrc;
    private StrategyGridAdapter mTypeAdapter;
    private StrategyGridAdapter mCharacterAdapter;
    private StrategyGridAdapter mSceneAdapter;
    private StrategyGridAdapter mStyleAdapter;

    @Override
    protected View inflateView() {
        return View.inflate(getActivity(), R.layout.fragment_classify_strategy, null);
    }

    @Override
    protected void initView(View view) {
        mTvStrategy = (TextView) view.findViewById(R.id.tv_strategy);
        mTvType = (TextView) view.findViewById(R.id.tv_strategy_type);
        mTvCharacter = (TextView) view.findViewById(R.id.tv_strategy_character);
        mTvScene = (TextView) view.findViewById(R.id.tv_strategy_scene);
        mTvStyle = (TextView) view.findViewById(R.id.tv_strategy_style);

        mSubRecyclerView = (RecyclerView) view.findViewById(R.id.recyc_sub_strategy);
        mTypeRecyclerView = (RecyclerView) view.findViewById(R.id.recyc_type_strategy);
        mCharacterRecyclerView = (RecyclerView) view.findViewById(R.id.recyc_character_strategy);
        mSceneRecyclerView = (RecyclerView) view.findViewById(R.id.recyc_scene_strategy);
        mStyleRecyclerView = (RecyclerView) view.findViewById(R.id.recyc_style_strategy);


    }

    @Override
    protected void initData() {



        //初始化专题的RecyclerView
        initSubRecyclerView();
        //下载专题的总Json数据
        loadSubData();

        //初始化品类的RecyclerView
        initTypeRecyclerView();
        //初始化对象的RecyclerView
        initCharacterRecyclerView();
        //初始化场合的RecyclerView
        initSceneRecyclerView();
        //初始化风格的RecyclerView
        initStyleRecyclerView();

        //下载ChannelGroups的总Json数据
        loadChannelGroupsData();

        setListener();

    }

    private void loadChannelGroupsData() {
        httpUtils.send(HttpRequest.HttpMethod.GET,
                Urls.CLASSIFY_CHANNELS,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        mCfcrc = gson.fromJson(result, ClassifyChannelsRecyc.class);
                        channelGroups = mCfcrc.getData().getChannel_groups();
                        mTvType.setText(channelGroups.get(0).getName());
                        mTvCharacter.setText(channelGroups.get(1).getName());
                        mTvScene.setText(channelGroups.get(2).getName());
                        mTvStyle.setText(channelGroups.get(3).getName());

                        Logger.i("__________" + mCfcrc.toString());
                        //设置品类适配器
                        mTypeAdapter = new StrategyGridAdapter(getContext(), mCfcrc, 0);
                        mTypeRecyclerView.setAdapter(mTypeAdapter);
                        //设置对象适配器
                        mCharacterAdapter = new StrategyGridAdapter(getContext(), mCfcrc, 1);
                        mCharacterRecyclerView.setAdapter(mCharacterAdapter);
                        //设置场合适配器
                        mSceneAdapter = new StrategyGridAdapter(getContext(), mCfcrc, 2);
                        mSceneRecyclerView.setAdapter(mSceneAdapter);
                        //设置风格适配器
                        mStyleAdapter = new StrategyGridAdapter(getContext(), mCfcrc, 3);
                        mStyleRecyclerView.setAdapter(mStyleAdapter);

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        ToastUtils.showShort(getContext(), "网络连接出错，请检查网络！");
                    }
                }
        );

    }

    private void loadSubData() {
        //创建请求对象
        httpUtils.send(HttpRequest.HttpMethod.GET,
                Urls.CLASSIFY_SUB,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        cfss = gson.fromJson(result, ClassifySubjectSc.class);
                        Logger.i("++++++++" + cfss.toString());
                        //设置专题适配器
                        mSubAdapter = new StrategyRecyclerViewAdapter(getContext(), cfss);
                        mSubRecyclerView.setAdapter(mSubAdapter);

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        ToastUtils.showShort(getContext(), "网络连接出错，请检查网络！");
                    }
                }
        );

    }

    private void setListener() {
        mTvStrategy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全部主题
                Intent intent = new Intent(getActivity(),CollectionRecylerActivity.class);
                startActivity(intent);
            }
        });
    }

    //专题
    private void initSubRecyclerView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mSubRecyclerView.setLayoutManager(linearLayoutManager);

    }

    //品类
    private void initTypeRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mTypeRecyclerView.setLayoutManager(gridLayoutManager);
    }

    //对象
    private void initCharacterRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mCharacterRecyclerView.setLayoutManager(gridLayoutManager);
    }
    //场合
    private void initSceneRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mSceneRecyclerView.setLayoutManager(gridLayoutManager);
    }
    //风格
    private void initStyleRecyclerView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mStyleRecyclerView.setLayoutManager(gridLayoutManager);
    }
}
